from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 加载数据
iris = load_iris()

X = iris.data
y = iris.target

# 做一个二分类逻辑回归
X = X[y<2]
y = y[y<2]

# 划分数据集 一部分作为训练集 一部分作为测试集
x_train, x_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=42)


# 创建逻辑回归模型
log_reg = LogisticRegression()

# 训练模型
log_reg.fit(x_train,y_train)

# 预测结果
y_pred = log_reg.predict(x_test)

# 计算准确率
accuracy=accuracy_score(y_test,y_pred)
print(f"模型准确率：{accuracy:.2f}")
